﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using ServiceSample.BankService;
using ClientBankWPF.Entities;
using ClientBankWPF.Service;
using CryptoModule;

namespace ServiceSample
{
    class Program
    {
        static void Main(string[] args)
        {
            //var client = new BankServiceClient();
            //Console.WriteLine("Enter");
            //string strVal = Console.ReadLine();
            //if (!string.IsNullOrEmpty(strVal))
            //{
            //    int value = Int32.Parse(strVal);
            //    Console.WriteLine(client.GetData(value));
            //    client.Close();
            //}
            //Console.Read();
            string name = Console.ReadLine();
            var privateKey = AddClient(name);
            AddPaymentOrder(privateKey);
            Console.ReadKey();
        }

        private static byte[] AddClient(string name)
        {
            var rsa = new RSASignature();
            var keys = rsa.GeneraitPair();
            var client = new Client
                             {
                                 Id = Guid.NewGuid(),
                                 Address = "qqq",
                                 UNP = 1,
                                 CFO = "aaa",
                                 Description = Encoding.ASCII.GetString(keys["private"]),
                                 OpenKey = keys["public"],
                                 Name = "pupkin2"
                             };
            var das = new DataAccessService();
            var message = das.AddClient(client);
            if (!String.IsNullOrEmpty(message)) Console.WriteLine(message);
            return keys["private"];
        }

        private static void AddPaymentOrder(byte[] privateKey)
        {
            var po = new PaymentOrder
                         {
                             Id = Guid.NewGuid(),
                             BenefBankCode = 1,
                             BenefBankName = "tratata",
                             Date = DateTime.Now,
                             BenefDepNum = 2,
                             Payer = "11",
                             PayerUNP = 1,
                             Benefeciar = "bb",
                             BenefUNP = 2,
                             Currency = 3,
                             PaymentAllocation = "bla",
                             Sum = 10,
                             SumCurrencyText = "11",
                             Urgent = true
                         };

            var das = new DataAccessService();
            //var client = das.GetClient("pupkin");
            //var privateKey = Encoding.ASCII.GetBytes(client.Description);

            var ms = new MemoryStream();
            var bf = new BinaryFormatter();
            bf.Serialize(ms, po);
            var data = ms.ToArray();

            var hash = MD5Crypto.MD5Encode(data);
            var rsa = new RSASignature();
            var eds = rsa.CreateSignature(hash, privateKey);
            var service = new BankServiceClient();
            var state = service.AddPaymentOrder(po, eds);
            Console.WriteLine(state.TransactionState);
        }


    }
}